﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace BabyCloudDrives.DesktopApp.Converters
{
    public class ImageTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string valueStr = value.ToString();
            Uri imageUri;
            const string uriFormat = "pack://application:,,,/Icons/{0}";
            switch (valueStr)
            {
                case "folder":
                    imageUri = new Uri(string.Format(uriFormat, "Fold_Types/folder.png"));
                    break;
                case "photo":
                case "album":
                    imageUri = new Uri(string.Format(uriFormat, "Fold_Types/pictures.png"));
                    break;
                case "video":
                    imageUri = new Uri(string.Format(uriFormat, "Fold_Types/movies.png"));
                    break;
                case "file":
                    imageUri = new Uri(string.Format(uriFormat, "Fold_Types/documents.png"));
                    break;
                default:
                    imageUri = new Uri(string.Format(uriFormat, "Fold_Types/folder.png"));
                    break;
            }
            return imageUri;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class FileImageTypeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null || values.Length < 2) return null;
            string fileName = values[0].ToString();
            string fileType = values[1].ToString();
            string fileExtName = Path.GetExtension(fileName);
            if (!string.IsNullOrEmpty(fileExtName) && fileExtName.Contains("."))
                fileExtName = fileExtName.Replace(".", string.Empty);
            const string uriFormat = "pack://application:,,,/Icons/{0}";

            string imageUriStr = string.Format(uriFormat, "File_Types/" + fileExtName + ".png");
            if (fileType == "folder")
                imageUriStr = string.Format(uriFormat, "Fold_Types/folder.png");
            Uri imageUri = new Uri(imageUriStr);
            if (!File.Exists(Path.Combine(Environment.CurrentDirectory, imageUri.LocalPath.TrimStart('/'))))
                imageUri = new Uri(string.Format(uriFormat, "File_Types/file.png"));
            return new BitmapImage(imageUri);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}